dynamic scoping การใช้
- Secondly, accessing variables under dynamic scoping is generally slower than under lexical scoping.
- It used dynamic scoping, not by choice but because nobody involved understood scoping.
- However, dynamic scoping has several disadvantages.
- Dynamic scoping is fairly easy to implement.
- In dynamic scoping, if a programmer declares a variable within the scope of a function, it is available to subroutines called from within that function.
- Common Lisp adopted lexical scoping from Scheme, as did Clojure, but some other dialects of Lisp, like Emacs Lisp, still use dynamic scoping.
- Since dynamic scoping is less common today, the Perl documentation warns that " local isn't what most people think of as local . ".
- Dynamic scoping provides an excellent abstraction for thread local storage, but if it is used that way it cannot be based on saving and restoring a global variable.
- Most modern languages use lexical scoping for variables and functions, though dynamic scoping is used in some languages, notably some dialects of Lisp, some " scripting " languages like Perl, and some template languages.
- Originally, this was intended as an optimization; lexical scoping was still uncommon and of uncertain performance . " I asked RMS when he was implementing emacs lisp why it was dynamically scoped and his exact reply was that lexical scope was too inefficient . " Dynamic scoping was also meant to provide greater flexibility for user customizations.
- The macro language itself only transforms the source code, without resolving names, but since the expansion is done in place, when the names in the expanded text are then resolved ( notably free variables ), they are resolved based on where they are expanded ( loosely " called " ), as if dynamic scoping were occurring.
- By contrast, if this language uses dynamic scoping, then g prints and modifies f's local variable x ( because g is called from within f ), so the program prints 3 and then 1 . ( As it happens, the language of the program is Bash, which uses dynamic scoping; so the program prints 3 and then 1 .)
- By contrast, if this language uses dynamic scoping, then g prints and modifies f's local variable x ( because g is called from within f ), so the program prints 3 and then 1 . ( As it happens, the language of the program is Bash, which uses dynamic scoping; so the program prints 3 and then 1 .)
- By contrast, in dynamic scoping, the scope extends to the " runtime context " of the function : local variables " stay in context " when another function is called, only moving out of context when the defining function ends, and thus local variables are in context of the function in which they are defined " and all called functions ".
- By contrast, in " dynamic scoping " ( or " dynamic scope " ), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing : while the function is running, the variable name exists, and is bound to its value, but after the function returns, the variable name does not exist.
- This means that if function f invokes a separately defined function g, then under lexical scoping, function g does " not " have access to f's local variables ( assuming the text of g is not inside the text of f ), while under dynamic scoping, function g " does " have access to f's local variables ( since g is invoked during the invocation of f ).